import React, { useState, useMemo, useEffect } from 'react';
import { ColView, filter_list, useHigh, GridTable, filter_row, useCmd, WaitUmd, MainView, obj_str, BtTxt, PanelFind,
    show_mt, no_del, Btn, is_empty, loc } from 'sui';
import { fix_col, load_col } from './cmd';
import { useAccess } from 'cache';
import { HisView } from './his';
import { DlgNum } from './ctl';

const BaseView = ({ title, mod, acc }) => {
    const [open_val, openVal] = useState(false);
    const [col, setCol] = useState();
    const [high] = useHigh(140, 100);
    const [no_acc] = useAccess();
    const umd = useCmd(`${mod}/list`);

    useEffect(() => {
        load_col(mod, setCol);
    }, [mod]);

    const getRow = useMemo(() => {
        const lst = filter_list(["sn"], col);
        return filter_row(umd.value, lst, umd.filter);
    }, [umd.value, umd.filter, col]);

    if (umd.isFin("his")) {
        return <HisView hreturn={umd.hreturn} col={col} mod={mod} title={title}/>;
    }

    const hDel = (d) => {
        if (no_del()) {
            return;
        }

        umd.update(`${mod}/rm`, {"k": obj_str(d, "sn")});
    };

    const act = {
        "add": (v, h) => umd.update(`${mod}/add`, {"v": v}, h),
        "cnum": (v) => umd.update(`${mod}/mditem`, {"v": {...v, sn: obj_str(umd.cur, "sn")}}),
    };

    const pact = {
        "cnum": () => openVal(true),
    };

    if (umd.isFin("col")) {
        const list = [
            {sn: mod, name: title, fix: fix_col()},
        ];

        const hreturn = () => {
            load_col(mod, setCol);
            umd.hreturn();
        };

        return <ColView hreturn={hreturn} title={title} subtitle={loc("l_221")} list={list} cur={mod}/>;
    }

    const getCol = () => {
        if (is_empty(col)) {
            return null;
        }

        const tmp = [
            {name: loc("l_193"), key: 'index', width: 70, frozen: true},
            {name: loc("l_238"), key: 'e_opt', width: 160, export: false, formatter: props => {
                const d = props.row;
                return (
                    <div className="inline-flex">
                        <BtTxt hcmd={() => umd.pact(pact, d, "cnum")} disabled={no_acc(acc)}>{loc("l_740")}</BtTxt>
                        <BtTxt hcmd={() => hDel(d)} disabled={no_acc(acc)}>{loc("l_237")}</BtTxt>
                    </div>
                );
            }},
        ];

        col.forEach(d => show_mt(tmp, d, 150));

        return tmp;
    };

    const Right = () => (
        <div className="inline-flex">
            <Btn className="mr-4" hcmd={() => umd.pact(null, null, "col")} disabled={no_acc(acc)}>{loc("l_221")}</Btn>
        </div>
    );

    const Left = () => {
        return (
            <div className="inline-flex items-center">
                {title}
                <Btn className="ml-8" hcmd={() => umd.pact(null, null, "his")} disabled={no_acc(acc)} color="teal">{loc("l_884")}</Btn>
            </div>
        );
    };

    return (
        <MainView>
            <PanelFind left={<Left/>} right={<Right/>} hcmd={v => umd.setFilter(v)}/>
            <GridTable col={getCol()} row={getRow} high={high}/>
            <DlgNum open={open_val} hcmd={v => umd.act(act, v)} setOpen={openVal} data={umd.cur}/>
            <WaitUmd umd={umd}/>
        </MainView>
    );
};

export {BaseView};
